package com.magicsoft.weitown.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.entity.EmployeeRightsEntity;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.StringUtils;
import com.magicsoft.constant.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class AppManngeActivity extends Activity implements OnClickListener {

	private DisplayImageOptions options;
	private ArrayList<EmployeeRightsEntity> homeMenuDatas = new ArrayList<EmployeeRightsEntity>();
	private HashMap<String, Boolean> isShowManage = new HashMap<String, Boolean>();
	private ListView mListView;
	private MenuManageListAdapter menuManageListAdapter;
	private ImageLoader imageLoader;
	private String tag = "AppManngeActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_mannge);
		initImageOptions();
		getCacheEmployee();
		initIsShow();
		initUI();
	}

	private void initUI() {
		imageLoader = ImageLoader.getInstance();
		menuManageListAdapter = new MenuManageListAdapter();
		mListView = (ListView) findViewById(R.id.listView1);
		mListView.setAdapter(menuManageListAdapter);
		findViewById(R.id.tv_save).setOnClickListener(this);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				String clientCode = homeMenuDatas.get(position).getClientCode();
				Boolean isshow = isShowManage.get(clientCode);
				isshow = !isshow;
				isShowManage.put(clientCode, isshow);
				menuManageListAdapter.notifyDataSetChanged();
				SharePreferenceHelper.setIsShowValue(AppManngeActivity.this,
						clientCode, isshow);
			}
		});
	}

	private void initIsShow() {
		for (int i = 0; i < homeMenuDatas.size(); i++) {
			String clientCode = homeMenuDatas.get(i).getClientCode();
			Boolean isshowValue = SharePreferenceHelper.getIsshowValue(
					AppManngeActivity.this, clientCode);
			// SharedPreferences sp = getSharedPreferences(clientCode, 0);
			// boolean isshow = sp.getBoolean("isshow", true);
			isShowManage.put(clientCode, isshowValue);
		}
	}

	// 获取本地缓存数据
	private void getCacheEmployee() {
		homeMenuDatas.clear();

		ArrayList<EmployeeRightsEntity> appList = SharePreferenceHelper
				.getAppList(this);

		if (appList != null) {
			homeMenuDatas.addAll(appList);
		}

	}

	private void initImageOptions() {
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.placeholder2)
				.showImageForEmptyUri(R.drawable.placeholder2)
				.showImageOnFail(R.drawable.placeholder2).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				// .displayer(new RoundedBitmapDisplayer(20))
				.build();
	}

	class MenuManageListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return homeMenuDatas.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			View view = null;
			EmployeeRightsEntity employeeRightsEntity = homeMenuDatas
					.get(position);

			if (convertView == null) {
				viewHolder = new ViewHolder();
				view = getLayoutInflater().inflate(
						R.layout.app_manage_listltem1, null);
				viewHolder.iv_image = (ImageView) view
						.findViewById(R.id.iv_image);
				viewHolder.tv_title = (TextView) view
						.findViewById(R.id.tv_title);
//				viewHolder.tv_content = (TextView) view
//						.findViewById(R.id.tv_content);
				viewHolder.ima_switch = (ImageView) view
						.findViewById(R.id.ima_switch);
				view.setTag(viewHolder);
			} else {
				view = convertView;
				viewHolder = (ViewHolder) view.getTag();
			}

			if (employeeRightsEntity != null) {
				String name = employeeRightsEntity.getName();

				viewHolder.tv_title.setText(name);
				if (StringUtils.isImageUrl(employeeRightsEntity.getIcon()
						.getAndroid())) {
					imageLoader.displayImage(employeeRightsEntity.getIcon()
							.getAndroid(), viewHolder.iv_image, options);
				}

			}
			String clientCode = employeeRightsEntity.getClientCode();
			Boolean isshow = isShowManage.get(clientCode);
			// Log.e("getView", "isshow ="+isshow);
			if (isshow) {
				viewHolder.ima_switch.setImageResource(R.drawable.btn_isshow);
			} else {
				viewHolder.ima_switch.setImageResource(R.drawable.btn1);
			}

			return view;
		}

	}

	class ViewHolder {
		ImageView iv_image;
		TextView tv_title;// 标题
//		TextView tv_content;// 内容
		ImageView ima_switch;// 开关
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_save:
			Intent data = new Intent();
			setResult(Constant.JumpManage_Code, data);
			finish();
			break;

		default:
			break;
		}
	}

}
